package com.itheima.controller.backend;


import com.itheima.DTO.CheckGroupDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
@Api(tags = "检查组管理")
public class CheckgroupController {
    @Autowired
    private CheckGroupService checkgroupService;


    /**
     *
     * @param checkgroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增")
    public Result add(@RequestBody CheckGroup checkgroup, String checkitemIds){
        log.info("新增检查组:{}",checkgroup.toString());
        List<String> idList = Arrays.asList(checkitemIds.split(","));
        log.info("新增检查组:{}",idList);

        checkgroupService.add(checkgroup,idList);
        return new Result(true,"新增检查组成功");
    }

    @GetMapping("/deleteById")
    @ApiOperation("删除检查组")
    public Result deleteById(Integer id){
        log.info("删除检查组:{}",id);
        checkgroupService.deleteById(id);
        return new Result(true,"删除检查组成功");
    }

    @GetMapping("/findCheckGroupIdsBySetmealId")
    @ApiOperation("根据套餐id查询检查项目id")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("根据套餐id查询检查项目id:{}",setmealId);
        List<Integer> checkGroupIds = checkgroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,"查询检查组成功",checkGroupIds);
    }

    @PostMapping("/findPage")
    @ApiOperation("分页查询检查组")
    public PageResult findPage(@RequestBody CheckGroupDTO checkGroup){
        log.info("分页查询检查组:{}",checkGroup.toString());

        PageResult pageResult = checkgroupService.pageQuery(checkGroup);
//        return new Result(true,"分页查询检查项成功",pageResult);
        return pageResult;
    }


    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查组")
    public Result findById(Integer id){
        log.info("根据id查询检查组:{}",id);
        CheckGroup checkGroup = checkgroupService.findById(id);
        if (checkGroup != null){
            return new Result(true,"查询检查组成功",checkGroup);
        }else {
            return new Result(false,"查询检查组失败");
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查组")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkgroupService.findAll();
        if (checkGroups != null){
            return new Result(true,"查询检查组成功",checkGroups);
        }else {
            return new Result(false,"查询检查组失败");
        }
    }

    @PostMapping("/edit")
    @ApiOperation("编辑检查组")
    public Result edit(@RequestBody CheckGroup checkgroup, String checkitemIds){
        log.info("编辑检查组:{}",checkgroup.toString());
        List<String> idList = Arrays.asList(checkitemIds.split(","));
        log.info("编辑检查组:{}",idList);

        checkgroupService.edit(checkgroup,idList);
        return new Result(true,"编辑检查组成功");
    }

}
